<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<table role="grid" aria-activedescendant="focused-cell" tabindex="-1" id="container-with-active-descendant">
  <tr role="row">
    <td role="gridcell" id="cell-with-active-descendant">Cell1</td>
    <td role="gridcell" id="clickable-cell-with-active-descendant">Cell2</td>
    <td role="gridcell" id="focused-cell">Cell3</td>
  </tr>
</table>
<div id="non-focusable-element">Div</div>
<button id="focusable-element">Button</button>

<script>
test(() => {
  if (document.activeElement)
    document.activeElement.blur();

    var container = document.getElementById('container-with-active-descendant');
  var accessibilityFocusable = document.getElementById('cell-with-active-descendant');
  var axFocusable = accessibilityController.accessibleElementById('cell-with-active-descendant');

  var gotEvent = false;
  container.addEventListener('click', () => {
  gotEvent = true;
  });
  axFocusable.takeFocus();

  assert_true(gotEvent);
  assert_not_equals(document.activeElement, accessibilityFocusable);
  accessibilityFocusable.display = 'none';
}, 'A click event should be dispatched if a non-focusable element can take accessibility focus via aria-activedescendant.');

test(() => {
  if (document.activeElement)
    document.activeElement.blur();

  var accessibilityFocusable = document.getElementById('clickable-cell-with-active-descendant');
  var axFocusable = accessibilityController.accessibleElementById('clickable-cell-with-active-descendant');

  var gotEvent = false;
  accessibilityFocusable.addEventListener('click', () => {
    gotEvent = true;
  });
  axFocusable.takeFocus();

  assert_false(gotEvent);
  assert_not_equals(document.activeElement, accessibilityFocusable);
  accessibilityFocusable.display = 'none';
}, 'A click event should not be dispatched if a click handler is attached directly to an element that can take accessibility focus via aria-activedescendant.');

test(() => {
  if (document.activeElement)
    document.activeElement.blur();

  var nonFocusable = document.getElementById('non-focusable-element');
  var axNonFocusable = accessibilityController.accessibleElementById('non-focusable-element');

  var gotEvent = false;
  nonFocusable.addEventListener('click', () => {
    gotEvent = true;
  });
  axNonFocusable.takeFocus();

  assert_false(gotEvent);
  assert_not_equals(document.activeElement, nonFocusable);
  nonFocusable.display = 'none';
}, 'A click event should not be dispatched if an element cannot take accessibility focus.');

test(() => {
  if (document.activeElement)
    document.activeElement.blur();

  var focusable = document.getElementById('focusable-element');
  var axFocusable = accessibilityController.accessibleElementById('focusable-element');

  var gotEvent = false;
  focusable.addEventListener('click', () => {
    gotEvent = true;
  });
  axFocusable.takeFocus();

  assert_false(gotEvent);
  assert_equals(document.activeElement, focusable);
  focusable.display = 'none';
}, 'A click event should not be dispatched if an element is focusable.');
</script>
